/************************************************************************************
* SimpleCollectionSortFilter.cpp                                                    *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "SimpleCollectionSortFilter.h"
#include "PartialCollectionModel.h"

#include <QString>
#include <QVariant>

using namespace Yamp;

SimpleCollectionSortFilter::SimpleCollectionSortFilter(
        YContext *context,
        QObject *parent) :
    QSortFilterProxyModel(parent),
    m_context(context),
    m_regExp(QString(""))
{

}


void SimpleCollectionSortFilter::setExpression(QString exp) {
    m_regExp = exp;
    invalidateFilter();
}

bool SimpleCollectionSortFilter::filterAcceptsRow(
    int source_row, const QModelIndex &source_parent) const {
//    PartialCollectionModel *model =  (PartialCollectionModel *) sourceModel();
    QString uuid = sourceModel()->data(sourceModel()->index(source_row, 0)).toString();
    CollectorItem* item = m_context->collector()->item(uuid);
    //TODO add tag based filters
    return item->title().contains(m_regExp) ||
           item->artist().contains(m_regExp)||
           item->album().contains(m_regExp);
    return true;
}



bool SimpleCollectionSortFilter::lessThan(
    const QModelIndex &left, const QModelIndex &right) const {
    QVariant lData = left.data();
    QVariant rData = right.data();
    if(lData.type() == QVariant::DateTime) {
        return lData.toDateTime() < rData.toDateTime();
    }
    else if(lData.type() == QVariant::Int) {
        return lData.toInt() < rData.toInt();
    }
    else if(lData.type() == QVariant::Double) {
        return lData.toDouble() < rData.toDouble();
    }
    return QString::localeAwareCompare(lData.toString(), rData.toString()) < 0;
}

